package com.anji.plus.gaea.inf.module.dynamic.controller;

import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.inf.constant.InfConstant;
import com.anji.plus.gaea.inf.module.dynamic.controller.dto.InfDynamicSqlDTO;
import com.anji.plus.gaea.inf.module.dynamic.controller.dto.SqlTestDemo;
import com.anji.plus.gaea.inf.module.dynamic.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ultrajiaming
 * @since 2021/3/18 14:55
 */
@RestController
@RequestMapping("custom/dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    @PostMapping("run/test")
    public ResponseBean runTest(@RequestBody InfDynamicSqlDTO dynamicSqlDto) {
        return ResponseBean.builder().data(dynamicService.runTest(dynamicSqlDto)).build();
    }

    @PostMapping("execute")
    public ResponseBean execute(HttpServletRequest request, @RequestBody InfDynamicSqlDTO dynamicSqlDto) {
        String appId = request.getHeader(InfConstant.REQUEST_HEADER_APP_ID);
        dynamicSqlDto.setAppId(appId);
        return ResponseBean.builder().data(dynamicService.execute(dynamicSqlDto)).build();
    }

    @PostMapping("testDemo")
    public ResponseBean testDemo(@RequestBody SqlTestDemo sqlTestDemo) {
        return ResponseBean.builder().data(dynamicService.testDemo(sqlTestDemo)).build();
    }

}
